package com.ysd.lis.service.sta.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.dto.sta.StaItemDictDto;
import com.ysd.lis.dto.sta.StaItemDictParam;
import com.ysd.lis.entity.sta.StaItemDict;
import com.ysd.lis.mapper.sta.StaItemDictMapper;
import com.ysd.lis.service.sta.StaItemDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * cold_chain.sys_module 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-12-27
 */
@Service
public class StaItemDictServiceImpl extends ServiceImpl<StaItemDictMapper, StaItemDict> implements StaItemDictService {

    @Autowired
    StaItemDictMapper staItemDictMapper;

    @Override
    public Result findStaItemDictList(StaItemDictParam pageParam) {
        MPJLambdaWrapper<StaItemDict> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.eq(StaItemDict::getDelFlag, 0);
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(pageParam.getWorkGroup()),StaItemDict::getWorkGroup, pageParam.getWorkGroup());
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(StaItemDict::getItemNo, pageParam.getSeachValue())
                    .or().like(StaItemDict::getItemNa, pageParam.getSeachValue())
                    .or().like(StaItemDict::getBasItemCode, pageParam.getSeachValue())
                    .or().like(StaItemDict::getItemEnCode, pageParam.getSeachValue())
                    .or().like(StaItemDict::getInputCode,pageParam.getSeachValue()));
        }
        StringBuffer existsSql = new StringBuffer("SELECT 1 FROM LAB_ITEM_DICT t2 WHERE t2.DEL_FLAG=0 and t.ITEM_NO = t2.ITEM_NO AND t2.RPT_GROUP =  '"+pageParam.getRptGroup()+"'");
        lambdaQueryWrapper.notExists(existsSql.toString());
        lambdaQueryWrapper.orderByAsc(StaItemDict::getSeq);
        List<StaItemDict> supplierPage = staItemDictMapper.selectList( lambdaQueryWrapper);
        return Result.succ(1, "查询成功", supplierPage);
    }

    @Override
    public Result addStaItemDict(StaItemDict staItemDict) {
        int insert = staItemDictMapper.insert(staItemDict);
        if(insert>0){
            return Result.succ(1,"添加成功",staItemDict);
        }else {
            return Result.fail(400,"添加失败","");
        }
    }

    @Override
    public Result editStaItemDict(StaItemDict supplier) {
        int updateById = staItemDictMapper.updateById(supplier);
        if(updateById>0){
            return Result.succ(1,"提交成功",supplier);
        }else {
            return Result.fail(400,"修改失败","");
        }
    }

    @Override
    public Result batchDeleteStaItemDictList(StaItemDictDto dto) {
        List<StaItemDict> supplierList = dto.getStaItemDictList();
        List<String> ids = supplierList.stream().map(StaItemDict::getId).collect(Collectors.toList());

        boolean b = this.removeByIds(ids);
        if(b){
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }


    @Override
    public Result findStaItemDictPageList(StaItemDictParam pageParam) {
        Page<StaItemDict> page = new Page<>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        MPJLambdaWrapper<StaItemDict> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.eq(StaItemDict::getDelFlag, 0);
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(pageParam.getWorkGroup()),StaItemDict::getWorkGroup, pageParam.getWorkGroup());
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(StaItemDict::getItemNo, pageParam.getSeachValue())
                    .or().like(StaItemDict::getItemNa, pageParam.getSeachValue())
                    .or().like(StaItemDict::getBasItemCode, pageParam.getSeachValue())
                    .or().like(StaItemDict::getItemEnCode, pageParam.getSeachValue())
                    .or().like(StaItemDict::getInputCode,pageParam.getSeachValue()));
        }

        lambdaQueryWrapper.orderByAsc(StaItemDict::getSeq);
        Page<StaItemDict> supplierPage = staItemDictMapper.selectPage(page, lambdaQueryWrapper);
        return Result.succ(1, "查询成功", supplierPage);
    }
}
